// pages/bindBankCard/bindBankCard.js
import imgUrl from "../wallet/imgUtil.js"
import {Wallet} from "../wallet/wallet.js"
import {getLogo} from "./cardConfig.js"
let app =getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardBlank:imgUrl.cardBlank,
    jiahao:imgUrl.jiahao,
    newDuigou:imgUrl.newDuigou,
    cardList:[
      //    {
      //    selected:false,
      //    id:1
      //  },{
      //    selected:false,
      //    id:2
      //  }
    ],
    openModal:false,
    currentBank:{},
    openPop:false,
    code:""

       
  },
  cancel(){
    // console.log(e)
    // console.log("000")
    this.setData({
      openPop:false
    })
  },
  getCode(e){
    console.log(e.detail)
    Wallet._cancelBankCar({bankCardId:this.data.currentBank.bankCardId,smsCode:e.detail+""},res=>{
      wx.showToast({
        title: '解绑成功',
      })
      this.setData({
        openPop:false,
        // code:e.detail
      })
      console.log(res)
    })
    // "bankCardId": 0,
    // "smsCode": ""
   
  },
  modifyBankCard(){
    // console.log(this.data.currentBank)
    this.setData({
      openModal:false
    })
    wx.navigateTo({
      url: '../setBankAccount/setBankAccount?id=1',
    })





  },
  hideModal(){
    this.setData({
      openModal:false
    })
  },
  selectBankCard(e){
    //  console.log(this.data.cardList)
    //  e.currentTarget.dataset.item.selected =true
     let arr=JSON.parse(JSON.stringify(this.data.cardList)) 
     arr.forEach((item,index)=>{
       item.selected=false
       if(item.bankCardId == e.currentTarget.dataset.item.bankCardId){
          item.selected =true
       }
     })
 
     app.globalData.tempBankCard=e.currentTarget.dataset.item

     this.setData({
       cardList:arr,
       openModal:true,
       currentBank:e.currentTarget.dataset.item
     })




  },
  useIt(){
    app.globalData.tempBankCard=this.data.currentBank
    this.setData({
      openModal:false
    })
    wx.navigateBack()
  },
  setDefault(){
     let currentBank=this.data.currentBank
  
     Wallet._setDefaltBank({bankCardId:currentBank.bankCardId + ""},res=>{
        console.log(res)
        wx.showToast({
          title: '设置成功',
          icon:"none",
        })
        this.setData({
          openModal:false
        })
        
        app.globalData.tempBankCard={}
        wx.navigateBack()

     })
  },
  unbind(){
      this.setData({
        openModal:false,
        openPop:true
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  jumpBanklist(){
      wx.navigateTo({
        url: '../setBankAccount/setBankAccount?id=2',
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(){
    Wallet._getBankCard(res=>{
//       bankCardId: 415
// bankLogo: "http://img.tiantue.com/uploads/icon/bank/jsyh.png"
// cardEndNo: "1541"
// cardHolder: "蔡蔡"
// cardNo: "4641329985656591541"
// isDefault: "0"
// remark: "测试3"
      // console.log(res)
      let list =res 
      getLogo((logoList,bankLogoList)=>{
          list.forEach(item =>{
                
             let bankType= logoList.filter(i=>{
              return i.bankType == item.bankType
             })[0].bankType
             if(item.isDefault == 1){
               item.selected =true
             }else{
               item.selected =false
             }
            
            item.logo=bankLogoList.filter(iconItem=>{
              return iconItem.id== bankType
            })[0].icon
            
          })
          this.setData({
          cardList:list
          })



      })

      
      

    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})